package com.example.kugou.commons.jianduo;

import com.alibaba.druid.support.json.JSONUtils;
import com.example.kugou.pojo.Sing;
import com.example.kugou.service.SingService;
import com.example.kugou.service.serviceimpl.SingServiceImpl;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 吴赵娣
 * @create 2020-01-14 15:05
 */
public class PinyinUtils {


    /**
     * 汉字转换为汉语拼音首字母，英文字符转化为首字母大写
     * @param a
     * @return
     */
    public static String getFirst(String a){
        String pinyinName = "";
        if (StringUtils.isNotBlank(a)){
            if (isChinese(a)){
                //是汉字
                char[] nameChar = a.toCharArray();
                HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
                defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
                defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                for (int i = 0; i < nameChar.length; i++) {
                    try {
                        pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                                nameChar[i], defaultFormat)[0].charAt(0);
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                        return null;
                    }
                }
                return pinyinName;
            }else if (isEnglish(a)){
                //不是汉字
                if (a.length()>=1){
                    pinyinName = a.substring(0,1).toUpperCase();
                    return pinyinName;
                }
            }else {
                return "1";
            }
        }
        return null;
    }
    private static boolean isChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;

        return flg;
    }
    private static boolean isEnglish(String str) {
        String regEx = "[A-Za-z]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;

        return flg;
    }

    public static void main(String[] args) {
        SingService singService = new SingServiceImpl();
        final String letter = PinyinUtils.getFirst("啊");
        System.out.println(letter);
        List<Sing> sings = singService.selectName(letter);
        if (sings.size() <= 0) {
            System.out.println(JSONUtils.toJSONString("没有要查询的数据"));
        }
        System.out.println(JSONUtils.toJSONString(sings));
    }



}
